const fs = require('fs')
const path = require('path')

function copyFolderRecursiveSync(source) {
	var folderPath = path.join(path.resolve(__dirname, source))
	fs.readdir(folderPath, (e, files) => {
		if (e) {
			console.error(`An error occurred while reading the directory: ${e}`)
			return
		}

		const jsFiles = files.filter((x) => path.extname(x) === '.js')
		jsFiles.forEach((file) => {
			fs.readFile(path.join(folderPath, file), 'utf-8', (readErr, content) => {
				if (readErr) {
					console.error(`An error occurred while reading the file ${file}: ${readErr}`)
					return
				}

				// 删除文件末尾的 'sourceMappingURL' 字符串
				const newContent = content.replace(`//# sourceMappingURL=${file}.map`, '').replace(/\r?\n$/, '')

				fs.writeFile(path.join(folderPath, file), newContent, (writeErr) => {
					if (writeErr) {
						console.error(`An error occurred while writing to the file ${file}: ${writeErr}`)
					}
				})
			})
		})
	})
}

copyFolderRecursiveSync(path.resolve(__dirname, './dist/assets'))
console.log('map ok')
